import { createSlice } from "@reduxjs/toolkit";
import dayjs from "dayjs";

export const CitySlice = createSlice({
  name: "CitySlice",
  initialState: {
    start: "北京",
    end: "广州",
    date: dayjs().format("YYYY-MM-DD"),
  },
  reducers: {
    //state===>initialState
    //payload==>调用方法传递的参数
    //修改城市
    editCity(state, { payload }) {
      //payload:{type:start || end city:...}
      state[payload.type] = payload.city;
    },
    //交换城市
    ExchangeCity(state) {
      let temp = state.start;
      state.start = state.end;
      state.end = temp;
    },
    //修改日期
    editDate(state, { payload }) {
      state.date = payload.date;
    },
  },
});

//抛出action
export const { editCity, ExchangeCity, editDate } = CitySlice.actions;

export default CitySlice.reducer;
